from ftllibs.runes import *
import operator
import copy
import sys
import re

class Runes():
    def __init__(self,parent):
        self.parent=parent
        self.RUNES={}
        self.RunesIndex=[]
        for i in sys.modules.keys():
            m = re.match('ftllibs.runes.(r_[a-z_.]+)',i)
            if m:
                self.RunesIndex.append(m.groups(0)[0])
        self.InitRunes()
        self.CreateLists()

    def InitRunes(self):
        for i in self.RunesIndex:
 #           rune = None
            cmd = "self.RUNES['%s']=%s.Rune()" % (i,i)
            exec cmd
#            self.RUNES[rune.Name]=rune
#        self.ITEMS['3001']=i3001.Item_3001()

    def GetRune(self,runename):
        return self.RUNES[runename]
        
    def GetRuneCopy(self,runename):
        return copy.deepcopy(self.RUNES[runename])

    def CreateLists(self):
        self.SortedLists={}
        for runename in self.RunesIndex:
            for i in self.RUNES[runename].Tags:
                if not self.SortedLists.has_key(i):
                    self.SortedLists[i]=[]
                    self.SortedLists[i].append(runename)
                else:
                    self.SortedLists[i].append(runename)
        for i in self.SortedLists:
            costs = {}
            #for var in collection:
            for j in self.SortedLists[i]:
                costs[j] = self.RUNES[j].Costs
            self.SortedLists[i] = []
            for k,l in sorted(costs.iteritems(), key=operator.itemgetter(1)):
                self.SortedLists[i].append(k)

    def GetList(self,string):
        #attack_speed
        #life_steal
        #spell_damage
        #armor
        #magic_resist
        #damage
        #health_regen
        #mana
        #health
        #movement
        #mana_regen
        #critical_strike
        #cooldown_reduction
        #armor_penetration
        #magic_penetration
        return self.SortedLists[string]
